package com.systemdevelopment.mainproject.View.NormalUser;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXTextField;
import com.systemdevelopment.mainproject.Model.BorrowRecord;
import com.systemdevelopment.mainproject.Model.Record;
import com.systemdevelopment.mainproject.View.mainUI;
import com.systemdevelopment.mainproject.View.oneInstanceController;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.ObservableSet;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.TextFieldTableCell;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;



public class PersonalRecordController extends oneInstanceController {

    @FXML
    private TableColumn<Record, String> isReturned;

    @FXML
    private JFXButton searchButton;

    @FXML
    private TableColumn<Record, String> ISBN;

    @FXML
    private JFXTextField searchField;

    @FXML
    private TableColumn<Record, String> BookName;

    @FXML
    private TableColumn<Record, String> borrower;

    @FXML
    private TableColumn<Record, String> startTime;

    @FXML
    private TableColumn<Record, String> endTime;

    @FXML
    private JFXButton showAllButton;

    @FXML
    private TableView<Record> table;

    @FXML
    private TableColumn<Record, String> returnTime;

    private ObservableList<Record> recordSet;

    private ObservableList<Record> tableItems;
    @FXML
    void showAll(ActionEvent event) {

    }

    @FXML
    void search(ActionEvent event) {

    }
    public void InitTable()
    {
        InitColumn();
        try {
            ArrayList<Record> records= BorrowRecord.getReaderRecord(mainUI.userController.getUser().getAccount());
            recordSet= FXCollections.observableList(records);
            tableItems=FXCollections.observableList(records);
            table.setItems(tableItems);

        } catch (SQLException e) {
            e.printStackTrace();
        }


    }
    protected  void InitColumn()
    {
        ISBN.setCellValueFactory(
                param -> new SimpleStringProperty(param.getValue().getISBN())
        );
        ISBN.setCellFactory(TextFieldTableCell.forTableColumn());

        BookName.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().getTitle()));
        BookName.setCellFactory(TextFieldTableCell.forTableColumn());

        borrower.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().getReader()));
        borrower.setCellFactory(TextFieldTableCell.forTableColumn());

        startTime.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().getStarttime()));
        startTime.setCellFactory(TextFieldTableCell.forTableColumn());

        endTime.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().getEndtime()));
        endTime.setCellFactory(TextFieldTableCell.forTableColumn());

        returnTime.setCellValueFactory(param ->
        {
            if(param.getValue().getState().equals("已归还"))
                return new SimpleStringProperty(param.getValue().getReturntime());
            else
                return new SimpleStringProperty("无");
        });
        returnTime.setCellFactory(TextFieldTableCell.forTableColumn());

        isReturned.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().getState()));
        isReturned.setCellFactory(TextFieldTableCell.forTableColumn());
    }

    public void returnBook(ActionEvent actionEvent) {
        Record record= table.getSelectionModel().getSelectedItem();
        try {
            BorrowRecord.Return(record.getReader(),record.getISBN());
            InitTable();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
